import {
  showFailModal,
  changeSystemLanguage,
  showAnimation,
} from '../../util/util';
const domain = require('../../util/api/index');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    currentLanguage: app.globalData.systemConfig.language,
    loginStatus: app.globalData.loginStatus,
    isShowSettingPopup: false, //设置弹窗
    showShare: false,
    themSwitch: app.globalData.systemConfig.theme == 'dark' ? true : false, //them setting
    name: wx.getStorageSync('name'),
    ShareOptions: [{
      name: '微信',
      icon: 'wechat',
      openType: 'share'
    }],
    Menu: [], //功能菜单
    animation: { //菜单过渡动画
      animation0: '',
      animation1: '',
      animation2: '',
      animation3: '',
      animation4: '', //loginOut button
      animation5: '', //about 
    },
    systemTheme: app.globalData.systemConfig.theme, //系统主题
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 国际化
    this.setLanguage();
    this.setData({
      name: wx.getStorageSync('name')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    showAnimation(this, 6, -380);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    showAnimation(this, 6, 380);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '加载中...',
    })
    setTimeout(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
    }, 500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //分享
  openShare() {
    this.setData({
      showShare: !this.data.showShare
    })
  },
  about() {
    wx.navigateTo({
      url: '/About/pages/index/index',
    })
  },
  loginOut() {
    this.changeLoginStatus(false);
  },
  gotoModal(val) {
    let item = val.currentTarget.dataset.item;
    if (app.globalData.loginStatus) {
      item.isLock ?
        showFailModal('暂无该菜单权限!') :
        wx.navigateTo({
          url: item.url,
        })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前未登录，请登录~',
        showCancel: false
      })
    }

  },
  //模拟登录，目前登录为假登录
  getphonenumber(res) {
    console.log(res);
    // if (res.detail.errMsg == "getPhoneNumber:ok") {
    this.changeLoginStatus(true);
    // } else {
    //   this.changeLoginStatus(false);
    //   showFailModal(res.detail.errMsg);
    // }
  },
  // 国际化
  setLanguage() {
    let Menu = app.globalData.Memu;
    let language = wx.T.getLanguage();
    Menu.forEach((item, index) => {
      item.name = language.menu[index][item.languageKey];
    });
    app.globalData.systemConfig.languageObj = language;
    this.setData({
      Menu,
      language
    });
  },

  openSettingPopup() {
    this.setData({
      isShowSettingPopup: !this.data.isShowSettingPopup
    });
  },
  changeLoginStatus(val) {
    app.globalData.loginStatus = val;
    wx.setStorageSync("loginStatus", Boolean(val));
    this.setData({
      loginStatus: Boolean(val)
    });
  },
  //change them setting
  changeThemSwitch({
    detail
  }) {
    this.setData({
      themSwitch: detail
    });
    this.changeTheme(detail ? 'dark' : 'light');
  },
  changeThemLanguage(val) {
    this.setData({
      currentLanguage: val.detail
    });
    app.globalData.systemConfig.language = val.detail;
    //change language
    changeSystemLanguage(val.detail);
    this.setLanguage();
  },
  //change system theme
  changeTheme(val) {
    app.globalData.systemConfig.theme = val;
    this.setData({
      systemTheme: val
    });
  }
})